﻿
// ReSharper disable CheckNamespace

using System;
using System.Web;

namespace NiuX.String;

public static partial class NiuXStringExtensions
{
    public static string ToHtmlDecode(this string str) => HttpUtility.HtmlDecode(str);

    public static string ToHtmlEncode(this string str) => HttpUtility.HtmlEncode(str);

    public static bool MayBeDateTimeOfYmd(this string str)
    {
        if (str.Length < 8)
        {
            return false;
        }

        if (int.TryParse(str.Substring(0, 4), out var year) && year > 1900 && int.TryParse(str.Substring(4, 2), out var month) && month is > 0 and < 13 && int.TryParse(str.Substring(6, 2), out var day) && day is > 0 and < 32)
        {
            return true;
        }
        // [132]={[dba_tmp_Pack_Item_n_n_yyyyMMdd, dba_tmp_Pack_Item_1_10016927_20211026180000]}
        return false;
    }

    public static string Left(string text, int leftLength) => string.IsNullOrEmpty(text) || text.Length <= leftLength ? text : text.Substring(0, leftLength);

    public static string Right(string text, int rightLength) => string.IsNullOrEmpty(text) || text.Length <= rightLength ? text : text.Substring(text.Length - rightLength, rightLength);
}